package org.wgc.study.controller;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wgc.study.job.*;

import java.util.Date;

;

/***
 *@title CallBatchJobController
 *@description <TODO description class purpose>
 *@author wgc_j
 *@version 1.0.0
 *@create 2025-08-04 15:06
 **/
@RestController
@RequestMapping("/call/batch")
public class CallBatchJobController {

 /*   @Autowired
    private BatchHelloWorldJob helloWorldJob;*/
    @Autowired
    private BatchMultiJob multiJob;
    @Autowired
    private BatchMultiStatusJob  multiStatusJob;
    @Autowired
    private JobLauncher jobLauncher;
    @Autowired
    private BatchFlowJob  batchFlowJob;

    @Autowired
    private  BatchSplitJob  batchSplitJob;
    @Autowired
    private BatchDeciderJob batchDeciderJob;

    @Autowired
    private  BatchSimpleItemReaderJob  batchSimpleItemReaderJob;

    @Autowired
    private  BatchFileItemReaderJob  batchFileItemReaderJob;

    @Autowired
    private  BatchDataSourceItemReaderJob  batchDataSourceItemReaderJob;
    /*@GetMapping("/hello/world")
    public Object helloWorld() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        // 根据时间配置每个job完成的使用参数，因为时间不一样所以参数不一样，所以任务可以重复执行
        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .toJobParameters();
        JobExecution run = jobLauncher.run(helloWorldJob.helloWorld(), jobParameters);
        BatchStatus status = run.getStatus();
        return status;
    }*/

    @GetMapping("/multiJob")
    public Object multiJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        // 根据时间配置每个job完成的使用参数，因为时间不一样所以参数不一样，所以任务可以重复执行
        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .toJobParameters();
        JobExecution run = jobLauncher.run(multiJob.multiJob(), jobParameters);
        BatchStatus status = run.getStatus();
        return status;
    }


    @GetMapping("/multiStatusJob")
    public Object multiStatusJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        // 根据时间配置每个job完成的使用参数，因为时间不一样所以参数不一样，所以任务可以重复执行
        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .toJobParameters();
        JobExecution run = jobLauncher.run(multiStatusJob.multiStatusJob(), jobParameters);
        BatchStatus status = run.getStatus();
        return status;
    }

    @GetMapping("/batchFlowJob")
    public Object batchFlowJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        // 根据时间配置每个job完成的使用参数，因为时间不一样所以参数不一样，所以任务可以重复执行
        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .toJobParameters();
        JobExecution run = jobLauncher.run(batchFlowJob.flowJob(), jobParameters);
        BatchStatus status = run.getStatus();
        return status;
    }

    @GetMapping("/batchSplitJob")
    public Object batchSplitJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        // 根据时间配置每个job完成的使用参数，因为时间不一样所以参数不一样，所以任务可以重复执行
        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .toJobParameters();
        JobExecution run = jobLauncher.run(batchSplitJob.splitJob(), jobParameters);
        BatchStatus status = run.getStatus();
        return status;
    }


    @GetMapping("/batchDeciderJob")
    public Object batchDeciderJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        // 根据时间配置每个job完成的使用参数，因为时间不一样所以参数不一样，所以任务可以重复执行
        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .toJobParameters();
        JobExecution run = jobLauncher.run(batchDeciderJob.deciderJob(), jobParameters);
        BatchStatus status = run.getStatus();
        return status;
    }

    @GetMapping("/batchFileItemReaderJob")
    public Object batchFileItemReaderJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        // 根据时间配置每个job完成的使用参数，因为时间不一样所以参数不一样，所以任务可以重复执行
        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .toJobParameters();
        JobExecution run = jobLauncher.run(batchFileItemReaderJob.fileItemReaderJob(), jobParameters);
        BatchStatus status = run.getStatus();
        return status;
    }

    @GetMapping("/batchDataSourceItemReaderJob")
    public Object batchDataSourceItemReaderJob() throws Exception, JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        // 根据时间配置每个job完成的使用参数，因为时间不一样所以参数不一样，所以任务可以重复执行
        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .toJobParameters();
        JobExecution run = jobLauncher.run(batchDataSourceItemReaderJob.dataSourceItemReaderJob(), jobParameters);
        BatchStatus status = run.getStatus();
        return status;
    }
    @Autowired
    private BatchXmlFileItemReaderJob batchXmlFileItemReaderJob;

    @GetMapping("/batchXmlFileItemReaderJob")
    public Object batchXmlFileItemReaderJob() throws Exception, JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        // 根据时间配置每个job完成的使用参数，因为时间不一样所以参数不一样，所以任务可以重复执行
        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .toJobParameters();
        JobExecution run = jobLauncher.run(batchXmlFileItemReaderJob.xmlFileItemReaderJob(), jobParameters);
        BatchStatus status = run.getStatus();
        return status;
    }

    @Autowired
    private ImportUserJob importUserJob;

    @GetMapping("/importUserJob")
    public Object importUserJob() throws Exception, JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        // 根据时间配置每个job完成的使用参数，因为时间不一样所以参数不一样，所以任务可以重复执行
        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .toJobParameters();
        JobExecution run = jobLauncher.run(importUserJob.job(), jobParameters);
        BatchStatus status = run.getStatus();
        return status;
    }

    @Autowired
    private ImportUserJob2 importUserJob2;

    @GetMapping("/importUserJob2")
    public Object importUserJob2() throws Exception, JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        // 根据时间配置每个job完成的使用参数，因为时间不一样所以参数不一样，所以任务可以重复执行
        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .toJobParameters();
        JobExecution run = jobLauncher.run(importUserJob2.csvJob(), jobParameters);
        BatchStatus status = run.getStatus();
        return status;
    }


 ;

    @Autowired
    Job myJob;

    @GetMapping("testJob")
    public  void testJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        //    后置参数：使用JobParameters中绑定参数 addLong  addString 等方法
        // 创建 JobParameters
        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .toJobParameters();

        // 启动 Job
        jobLauncher.run(myJob, jobParameters);

    }
}
